package cn.wonderland.basic.controller;

import cn.wonderland.basic.dto.EmailRegistDto;
import cn.wonderland.basic.dto.PhoneRegistDto;
import cn.wonderland.basic.service.VerifyCodeService;
import cn.wonderland.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.IOException;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private VerifyCodeService verifyCodeService;

    // 获取图形验证码
    @GetMapping("/image/{imageCodeKey}")
    public AjaxResult getImageCode(@PathVariable("imageCodeKey") String imageCodeKey) throws IOException {
        String imageCode = verifyCodeService.getImageCode(imageCodeKey);
        return AjaxResult.me().success(imageCode);
    }

    // 获取图形验证码--邮箱
    @GetMapping("/image/email/{emailImageCodeKey}")
    public AjaxResult getEmailImageCode(@PathVariable("emailImageCodeKey") String emailImageCodeKey) throws IOException {
        String imageCode = verifyCodeService.getEmailImageCode(emailImageCodeKey);
        return AjaxResult.me().success(imageCode);
    }

    // 获取手机短信验证码
    @PostMapping("/smsCode")
    public AjaxResult smsCode(@RequestBody @Valid PhoneRegistDto dto) {
       verifyCodeService.smsCode(dto);
        return AjaxResult.me().success();
    }

    // 获取邮箱验证码
    @PostMapping("/emailCode")
    public AjaxResult emailCode(@RequestBody @Valid EmailRegistDto dto) {
       verifyCodeService.emailCode(dto);
        return AjaxResult.me().success();
    }
}
